package com.xiaochong.tir.apiservice.component;

import akka.actor.UntypedActor;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.tir.apiservice.component.hbase.CurrencyInfoHbaseComponent;
import com.xiaochong.tir.apiservice.util.SpringContextUtil;
import com.xiaochong.tir.common.data.hbasepo.*;
import com.xiaochong.tir.common.util.AkkaParamUtils;
import com.xiaochong.tir.common.util.LogTraceKeysUtils;

import java.util.List;
import java.util.Map;

/**
 * Created by wujiaxing on 2018/5/7.
 */
public class AkkaComprehensiveAnalysis extends UntypedActor {


    private CmcDataCalculateComponent cmcDataCalculateComponent =
            (CmcDataCalculateComponent) SpringContextUtil.getBean("cmcDataCalculateComponent");


    private CmcDataCountCalculateComponent cmcDataCountCalculateComponent =
            (CmcDataCountCalculateComponent) SpringContextUtil.getBean("cmcDataCountCalculateComponent");

    private CurrencyInfoHbaseComponent currencyInfoHbaseComponent =
            (CurrencyInfoHbaseComponent) SpringContextUtil.getBean("currencyInfoHbaseComponent");

    @Override
    public void onReceive(Object o) {
        LogTrace.beginTrace("综合分析计算");
        Object o1 = ((Object[]) o)[0];
        Map<String,Object> akkaComprehensiveAnalysisMap = (Map<String, Object>) o1;

        List<CmcCurrencyInfo> cmcCurrencyInfos =
                (List<CmcCurrencyInfo>) akkaComprehensiveAnalysisMap.get(AkkaParamUtils.CMC_CURRENCY_INFO_LIST);

        Map<String, CmcCurrencyInfo> cmcMap =
                (Map<String, CmcCurrencyInfo>) akkaComprehensiveAnalysisMap.get(AkkaParamUtils.CMC_CURRENCY_INFO_MAP);

        List<DailyComprehensiveAnalysisInfo> dailyComprehensiveAnalysisInfos =
                (List<DailyComprehensiveAnalysisInfo>) akkaComprehensiveAnalysisMap.get(AkkaParamUtils.DAILY_COMPREHENSIVE_ANALYSIS_INFO_LIST);

        Map<String, DailyComprehensiveAnalysisInfo> dailyInfoMap =
                (Map<String, DailyComprehensiveAnalysisInfo>) akkaComprehensiveAnalysisMap.get(AkkaParamUtils.DAILY_COMPREHENSIVE_ANALYSIS_INFO_MAP);


        //计算涨跌
        CurrentcyHighOrLow currentcyHighOrLow = cmcDataCountCalculateComponent.HighsAndLowsCountCalculate(cmcCurrencyInfos);
        System.out.println(currentcyHighOrLow);
        if(currentcyHighOrLow != null){
            //插入涨跌
            cmcDataCountCalculateComponent.insertHbaseHighsAndLowsCountCalculate(currentcyHighOrLow);

        }
        LogTrace.info(LogTraceKeysUtils.AKKA_COMPREHENSIVE_ANALYSIS_TITLE, LogTraceKeysUtils.DATA,"大盘强度综合分析计算开始");
        if(currentcyHighOrLow==null){
            currentcyHighOrLow = currencyInfoHbaseComponent.getCurrentcyHighOrLow();
        }

        MoneyEffect moneyEffect = null;
        try{
            moneyEffect = cmcDataCalculateComponent.calculateMoneyEffect(dailyInfoMap);
        }catch (Exception e) {
            e.printStackTrace();
            LogTrace.error(e.getMessage(),e);
        }
        LogTrace.info(LogTraceKeysUtils.AKKA_COMPREHENSIVE_ANALYSIS_TITLE,LogTraceKeysUtils.DATA, "赚钱效益计算完成");
        if(moneyEffect==null){
            moneyEffect = currencyInfoHbaseComponent.getLastMoneyEffect();
        }else{
            currencyInfoHbaseComponent.saveMoneyEffect(moneyEffect);
        }


        WeightsPerformance weightsPerformance = null;
        try{
            weightsPerformance = cmcDataCalculateComponent.calculateWeightsPerformance(dailyComprehensiveAnalysisInfos);
        }catch (Exception e) {
            e.printStackTrace();
            LogTrace.error(e.getMessage(),e);
        }
        LogTrace.info(LogTraceKeysUtils.AKKA_COMPREHENSIVE_ANALYSIS_TITLE,LogTraceKeysUtils.DATA, "权重表现计算完成");
        if(weightsPerformance==null){
            weightsPerformance = currencyInfoHbaseComponent.getLastWeightsPerformance();
        }


        QuantumEnergy quantumEnergy=null;
        try{
            quantumEnergy = cmcDataCalculateComponent.calculateQuantumEnergy(dailyInfoMap);
            LogTrace.info(LogTraceKeysUtils.AKKA_COMPREHENSIVE_ANALYSIS_TITLE,LogTraceKeysUtils.DATA, "量能变化计算完成");
        }catch (Exception e) {
            e.printStackTrace();
            LogTrace.error(e.getMessage(),e);
        }
        if(quantumEnergy==null){
            quantumEnergy = currencyInfoHbaseComponent.getLastQuantumEnergy();
        }

        cmcDataCalculateComponent.insertDailyCurrencyInfor(currentcyHighOrLow,moneyEffect,weightsPerformance,quantumEnergy);
        LogTrace.endTrace();
    }
}
